﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace Urbania.GUI
{
    class Button
    {
        public enum DesignType { Type1 = 1, Type2 };

        protected Rectangle bounds;
        protected String text;
        protected DesignType designType;
        protected Texture2D sprite;
        protected SpriteFont font;
        protected Color fontColor;
        protected String scriptFilePath;
        protected LuaVM lua;

        public Button(Vector2 position, String text, int designType, ref LuaVM lua, string scriptPath)
        {
            this.sprite = Utilities.Loader.GetTex("button" + (int)designType);
            this.font = Utilities.Loader.GetFont("sf");
            this.bounds = new Rectangle((int)position.X, (int)position.Y, (int)(font.MeasureString(text).X + 10), (int)(font.MeasureString(text).Y + 10));
            this.text = text;
            this.designType = (DesignType)designType;
            this.fontColor = Color.White;
            this.lua = lua;
            this.scriptFilePath = scriptPath;
            Utilities.Loader.LoadScriptFile(this.scriptFilePath);
        }

        public void Update(GameTime gameTime, MouseState mouse)
        {
            if (mouse.X >= bounds.Right && mouse.X <= bounds.X)
                if(mouse.Y >= bounds.Bottom && mouse.Y <= bounds.Y)
                    this.fontColor = Color.Yellow;
            else
                this.fontColor = Color.White;
        }

        public void Update(GameTime gameTime, Vector2 mouse, bool mouseClicked)
        {
            if (mouse.X <= bounds.Right && mouse.X >= bounds.X)
                if (mouse.Y <= bounds.Bottom && mouse.Y >= bounds.Y)
                    this.fontColor = Color.Yellow;
                else
                    this.fontColor = Color.White;
            else
                this.fontColor = Color.White;

            if (mouseClicked)
            {
                this.OnClick();
            }
        }

        public void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(sprite, bounds, Color.White);
            spriteBatch.DrawString(font, text, new Vector2(2 + bounds.X, 5 + bounds.Y),fontColor);
        }

        public void OnClick()
        {
            string tempString = Utilities.Loader.GetScriptFile(scriptFilePath);
            lua.LuaVirtual.DoFile(tempString);
        }
    }
}
